package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caifangyi
 * @date 2022/8/16
 */

public class Day1656 {
    public static void main(String[] args) {
        OrderedStream obj = new OrderedStream(5);
        List<String> param_1 = obj.insert(3,"ccccc");
        System.out.println(param_1.stream().collect(Collectors.joining(",")));

    }

    static class OrderedStream {

        String[] arr;
        int ptr = 1;
        int capacity;
        public OrderedStream(int n) {
            this.capacity = n;
            arr = new String[n+1];
        }

        public List<String> insert(int idKey, String value) {
            if(idKey>capacity){
                return Collections.EMPTY_LIST;
            }
            arr[idKey] = value;
            if(ptr != idKey){
                return Collections.EMPTY_LIST;
            }
            List<String> objects = new ArrayList<>();
            for (int i = idKey; i < arr.length; i++) {
                if(arr[i] == null){
                    break;
                }
                objects.add(arr[i]);
                ptr++;
            }
            return objects;
        }
    }

/**
 * Your OrderedStream object will be instantiated and called as such:
 * OrderedStream obj = new OrderedStream(n);
 * List<String> param_1 = obj.insert(idKey,value);
 */
}
